package com.lxl.bt.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import androidx.viewpager.widget.ViewPager;

import com.flyco.tablayout.SlidingTabLayout;
import com.lxl.bt.R;
import com.lxl.bt.activity.BaseFragment;
import com.lxl.bt.adapter.MessageBootFragmentAdapter;
import com.lxl.bt.bean.NearBox;
import com.lxl.bt.bean.PromoCategory;
import com.lxl.bt.util.ACache;
import com.lxl.bt.view.CustomViewPager;
import com.yanzhenjie.sofia.Sofia;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Create by lvxinlong
 * on 消息
 * on 2020年12月5日08:47:54
 **/
public class MessageFragment extends BaseFragment {


    @BindView(R.id.rlBack)
    RelativeLayout rlBack;
    @BindView(R.id.txt_toolbar_title)
    TextView txtToolbarTitle;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.tab_layout)
    SlidingTabLayout tabLayout;
    @BindView(R.id.view_pager)
    CustomViewPager viewPager;
    private Unbinder unbinder;
    ACache aCache;
    private NearBox nearBox;
    private List<PromoCategory> spreadCategoryList = new ArrayList<>();
    private int current;
    private List<String> promoCategoryList = new ArrayList<>();
    MessageBootFragmentAdapter selectionCenterFragmentAdapter;

    public static MessageFragment newInstance() {
        Bundle bundle = new Bundle();
        MessageFragment fragment = new MessageFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_message, container, false);
//        ((BaseActivity) getActivity()).setStatusBarFullTransparent();

        //返回一个Unbinder值（进行解绑），注意这里的this不能使用getActivity()
        unbinder = ButterKnife.bind(this, view);
        viewPager.setCanPageScroll(false);
//        EventBus.getDefault().register(this);
        aCache = ACache.get(getActivity());
//        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, ScreenUtils.getScreenWidth() / 2);
//        banner.setLayoutParams(layoutParams);
        //修改切换动画
//        banner.setPageTransformer(Transformer.Default);
        Sofia.with(getActivity()) .statusBarBackgroundAlpha(0)
                .invasionStatusBar();
//                .statusBarLightFont();
//                .statusBarDarkFont()
        return view;
    }


    @Override
    public void onResume() {
        super.onResume();

    }




    @Override
    public void getData() {
    }


    /**
     * onDestroyView中进行解绑操作
     */
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        refFragmentView();
    }

    void refFragmentView() {
        promoCategoryList.add("系统消息");
        promoCategoryList.add("设备报警");
        promoCategoryList.add("车辆报警");
        promoCategoryList.add("人员报警");
        selectionCenterFragmentAdapter = new  MessageBootFragmentAdapter(getChildFragmentManager(), promoCategoryList);
        viewPager.setAdapter(selectionCenterFragmentAdapter);
        tabLayout.setViewPager(viewPager);
        viewPager.setCurrentItem(current);
        viewPager.setOffscreenPageLimit(promoCategoryList.size());


        refTableTitleView();
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                current = position;
                refTableTitleView();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void refTableTitleView() {
        for (int i = 0; i < promoCategoryList.size(); i++) {
            if (i == current) {
                tabLayout.getTitleView(current).getPaint().setFakeBoldText(true);
            } else {
                tabLayout.getTitleView(i).getPaint().setFakeBoldText(false);
            }
        }


    }


}
